package id.co.gomobile.assessment

import org.codehaus.groovy.grails.plugins.springsecurity.Secured;

class ItemOptionController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	@Secured(["ROLE_ADMIN"])
    def create = {
        def itemOptionInstance = new ItemOption()
        itemOptionInstance.properties = params
		if(itemOptionInstance.item == null) {
			flash.message = "${message(code: 'item.empty.message', default: 'Invalid item, please add option from the item details page.')}"
			redirect(controller: "test", action: "list")
		}
        return [itemOptionInstance: itemOptionInstance]
    }

	@Secured(["ROLE_ADMIN"])
    def save = {
        def itemOptionInstance = new ItemOption(params)
        if (itemOptionInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), itemOptionInstance.id])}"
            redirect(controller: "item", action: "show", id: itemOptionInstance.item.id)
        }
        else {
            render(view: "create", model: [itemOptionInstance: itemOptionInstance])
        }
    }

	@Secured(["ROLE_ADMIN"])
    def edit = {
        def itemOptionInstance = ItemOption.get(params.id)
        if (!itemOptionInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), params.id])}"
			redirect(controller: "test", action: "list")
        }
        else {
            return [itemOptionInstance: itemOptionInstance]
        }
    }

	@Secured(["ROLE_ADMIN"])
    def update = {
        def itemOptionInstance = ItemOption.get(params.id)
        if (itemOptionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (itemOptionInstance.version > version) {
                    
                    itemOptionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'itemOption.label', default: 'ItemOption')] as Object[], "Another user has updated this ItemOption while you were editing")
                    render(view: "edit", model: [itemOptionInstance: itemOptionInstance])
                    return
                }
            }
            itemOptionInstance.properties = params
            if (!itemOptionInstance.hasErrors() && itemOptionInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), itemOptionInstance.id])}"
				redirect(controller: "item", action: "show", id: itemOptionInstance.item.id)
            }
            else {
                render(view: "edit", model: [itemOptionInstance: itemOptionInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), params.id])}"
			redirect(controller: "test", action: "list")
        }
    }

	@Secured(["ROLE_ADMIN"])
    def delete = {
        def itemOptionInstance = ItemOption.get(params.id)
        if (itemOptionInstance) {
            try {
                itemOptionInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), params.id])}"
				redirect(controller: "item", action: "show", id: itemOptionInstance.item.id)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), params.id])}"
				redirect(controller: "item", action: "show", id: itemOptionInstance.item.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'itemOption.label', default: 'ItemOption'), params.id])}"
            redirect(controller: "test", action: "list")
        }
    }
}
